<template>
  <div class="custom-modal" v-if="visible" @click="handleOverlayClick">
    <div class="modal-overlay"></div>
    <div class="modal-content">
      <div class="modal-header">
        <div class="modal-icon">
          <i :class="iconClass"></i>
        </div>
        <h3 class="modal-title">{{ title }}</h3>
      </div>
      <div class="modal-body">
        <p class="modal-message">{{ message }}</p>
      </div>
      <div class="modal-footer">
        <button 
          v-if="type === 'confirm'"
          class="btn btn-ghost" 
          @click="handleCancel"
        >
          取消
        </button>
        <button 
          class="btn"
          :class="confirmButtonClass"
          @click="handleConfirm"
        >
          {{ confirmText }}
        </button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'CustomModal',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    type: {
      type: String,
      default: 'alert', // 'alert' or 'confirm'
      validator: value => ['alert', 'confirm'].includes(value)
    },
    title: {
      type: String,
      default: '提示'
    },
    message: {
      type: String,
      required: true
    },
    variant: {
      type: String,
      default: 'info', // 'info', 'warning', 'error', 'success'
      validator: value => ['info', 'warning', 'error', 'success'].includes(value)
    },
    confirmText: {
      type: String,
      default: '确定'
    }
  },
  emits: ['confirm', 'cancel', 'close'],
  computed: {
    iconClass() {
      const icons = {
        info: 'fas fa-info-circle text-blue',
        warning: 'fas fa-exclamation-triangle text-yellow',
        error: 'fas fa-times-circle text-red',
        success: 'fas fa-check-circle text-green'
      }
      return icons[this.variant] || icons.info
    },
    confirmButtonClass() {
      const classes = {
        info: 'btn-primary',
        warning: 'btn-warning',
        error: 'btn-danger',
        success: 'btn-success'
      }
      return classes[this.variant] || 'btn-primary'
    }
  },
  methods: {
    handleConfirm() {
      this.$emit('confirm')
      this.$emit('close')
    },
    handleCancel() {
      this.$emit('cancel')
      this.$emit('close')
    },
    handleOverlayClick(e) {
      if (e.target.classList.contains('custom-modal') || e.target.classList.contains('modal-overlay')) {
        if (this.type === 'confirm') {
          this.handleCancel()
        } else {
          this.handleConfirm()
        }
      }
    }
  },
  mounted() {
    // Prevent body scroll when modal is open
    document.body.style.overflow = 'hidden'
  },
  beforeUnmount() {
    // Restore body scroll
    document.body.style.overflow = ''
  }
}
</script>

<style scoped>
.custom-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.modal-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
}

.modal-content {
  position: relative;
  background: white;
  border-radius: 16px;
  max-width: 420px;
  width: 100%;
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
  animation: modalSlideIn 0.3s ease-out;
}

@keyframes modalSlideIn {
  from {
    opacity: 0;
    transform: scale(0.9) translateY(-20px);
  }
  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

.modal-header {
  padding: 2rem 2rem 1rem 2rem;
  text-align: center;
}

.modal-icon {
  margin-bottom: 1rem;
}

.modal-icon i {
  font-size: 48px;
}

.text-blue { color: #3b82f6; }
.text-yellow { color: #f59e0b; }
.text-red { color: #ef4444; }
.text-green { color: #10b981; }

.modal-title {
  font-size: 18px;
  font-weight: 600;
  color: #1e293b;
  margin: 0;
}

.modal-body {
  padding: 0 2rem 0rem 2rem;
  text-align: center;
  min-height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-message {
  font-size: 14px;
  color: #64748b;
  line-height: 1.6;
  margin: 0;
}

.modal-footer {
  padding: 1.5rem 2rem 2rem 2rem;
  display: flex;
  gap: 1rem;
  justify-content: center;
}

.btn {
  padding: 0.75rem 2rem;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  border: 1px solid transparent;
  min-width: 80px;
}

.btn-primary {
  background: #3b82f6;
  color: white;
  border-color: #3b82f6;
}

.btn-primary:hover {
  background: #2563eb;
  border-color: #2563eb;
}

.btn-warning {
  background: #f59e0b;
  color: white;
  border-color: #f59e0b;
}

.btn-warning:hover {
  background: #d97706;
  border-color: #d97706;
}

.btn-danger {
  background: #ef4444;
  color: white;
  border-color: #ef4444;
}

.btn-danger:hover {
  background: #dc2626;
  border-color: #dc2626;
}

.btn-success {
  background: #10b981;
  color: white;
  border-color: #10b981;
}

.btn-success:hover {
  background: #059669;
  border-color: #059669;
}

.btn-ghost {
  background: transparent;
  color: #64748b;
  border-color: #e2e8f0;
}

.btn-ghost:hover {
  background: #f8fafc;
  color: #1e293b;
  border-color: #cbd5e1;
}

@media (max-width: 480px) {
  .modal-content {
    max-width: 90vw;
    margin: 1rem;
  }
  
  .modal-header,
  .modal-body,
  .modal-footer {
    padding-left: 1.5rem;
    padding-right: 1.5rem;
  }
  
  .modal-footer {
    flex-direction: column;
  }
  
  .btn {
    width: 100%;
  }
}
</style>